import {Source} from "../source/Source";
import {FreeMap} from "../map/FreeMap";
import {FeatureCollection} from "@turf/helpers";
import {MapScene} from "../render/MapScene";
import {CRS} from "../crs/CRS";
import {Style} from "./style/Style";

/**
 * 绘图图层接口
 */
export abstract class Layer {
    /**
     * 图层id
     */
    public layerId: string;

    public sourceId: string;

    protected source: Source;

    protected map: FreeMap;

    protected scene: MapScene;

    protected crs: CRS;

    public style: Style;

    constructor(layerId: string, sourceId: string) {
        this.layerId = layerId;
        this.sourceId = sourceId;
    }

    public addTo(map: FreeMap) {
        this.map = map;

        let source = null;

        for (source of this.map.sources) {
            if (source.sourceId == this.sourceId) {
                break
            }
        }

        if (source != null) {
            source.connectLayer(this);
            this.source = source;
            this.scene = map.mapRenderer.scene;
            this.crs = map.crs;
        } else {
            console.error("source %s not found", this.sourceId)
        }
    }

    /**
     * 当关联的数据源数据变化时回调
     */
    abstract onSourceDataChange(data: any): void
}
